from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

# 准备options


options = webdriver.ChromeOptions()
user_data_dir = r'--user-data-dir=C:\Users\77575\AppData\Local\Google\Chrome\User Data'
options.add_argument(user_data_dir)

# 打开浏览器
driver = webdriver.Chrome(options=options)
# 请求网址
driver.get('https://mail.qq.com/')

# 返回主页面
driver.switch_to.default_content()

# 定位写信
sleep(2)
write_letter = driver.find_element(By.LINK_TEXT, '写信')
write_letter.click()

# 切换到frame mainFrame
driver.switch_to.frame('mainFrame')

# 通过XPATH://*[@id="toAreaCtrl"]/div[2]/input定位收件人输入框
sleep(2)
driver.find_element(By.XPATH, '//*[@id="toAreaCtrl"]/div[2]/input').send_keys('2498626243@qq.com')

# 主题id:subject
driver.find_element(By.ID, 'subject').send_keys('第1次测试自动发送邮件')

# 先定位正文的frame,在切换到正文标签页面
text_webelement = driver.find_element(By.XPATH, '//*[@class="qmEditorIfrmEditArea"]')
driver.switch_to.frame(text_webelement)

# 定位正文:Xpath:/html/body,先点击在输入
content = driver.find_element(By.XPATH, '/html/body')
content.click()
content.send_keys('hello1')

# 先切换会父页面,再通过link_text:发送定位发送按钮
driver.switch_to.parent_frame()
driver.find_element(By.LINK_TEXT, '发送').click()

# 退出浏览器
sleep(5)
driver.quit()
